/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conn.dao;

import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author kuma-chin
 */
public class DSesion extends DBase {

    public boolean permitidosSesion(String usr, String passwd) {
        boolean validar = false;
        String clave = "";
        String query = "";
        Statement stmt = null;
        ResultSet res = null;
        this.init_mysql();  // se inicia la conección a la base de datos

        try {
            stmt = conn.createStatement();
            query = "SELECT idEmpleado, password FROM empleados where username='" + usr + "'";
            res = stmt.executeQuery(query);
            if (res.next()) {
                clave = res.getString(2);
            }
            if (clave.equals(passwd)) {
                validar = true;
                this.newSesion(res.getInt(1));
            }

        } catch (SQLException ex) {
            System.out.println("SqlException : " + ex.toString());
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.toString());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    ;
                }
                stmt = null;
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    ;
                }
                conn = null;
            }
        }

        return validar;
    }

    public void newSesion(int idemp) {
        String query = null;
        Statement stmt = null;
        this.init_mysql();  // se inicia la conección a la base de datos

        try {
            stmt = conn.createStatement();
            query = "INSERT INTO Sesion (idEmpleado) VALUES ("+idemp+")";
            stmt.executeUpdate(query);
            stmt.close();
            conn.close();
        } catch (SQLException ex) {
            System.out.println("SqlException : " + ex.toString());
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.toString());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    ;
                }
                stmt = null;
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    ;
                }
                conn = null;
            }
        }
    }
}
